home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Math / acos.c next >
C/C++ Source or Header  |  1990-04-07  |  4KB  |  124 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                              N O T I C E                             *
  4.  *                                                                      *
  5.  *                      Copyright Abandoned, 1987, Fred Fish            *
  6.  *                                                                      *
  7.  *      This previously copyrighted work has been placed into the       *
  8.  *      public domain by the author (Fred Fish) and may be freely used  *
  9.  *      for any purpose, private or commercial.  I would appreciate     *
  10.  *      it, as a courtesy, if this notice is left in all copies and     *
  11.  *      derivative works.  Thank you, and enjoy...                      *
  12.  *                                                                      *
  13.  *      The author makes no warranty of any kind with respect to this   *
  14.  *      product and explicitly disclaims any implied warranties of      *
  15.  *      merchantability or fitness for any particular purpose.          *
  16.  *                                                                      *
  17.  ************************************************************************
  18.  */
  19.  
  20. /*
  21.  *  FUNCTION
  22.  *
  23.  *      acos   double precision arc cosine
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *      acos
  28.  *      machine independent routines
  29.  *      trigonometric functions
  30.  *      math libraries
  31.  *
  32.  *  DESCRIPTION
  33.  *
  34.  *      Returns double precision arc cosine of double precision
  35.  *      floating point argument.
  36.  *
  37.  *  USAGE
  38.  *
  39.  *      double acos (x)
  40.  *      double x;
  41.  *
  42.  *  REFERENCES
  43.  *
  44.  *      Fortran IV-plus user's guide, Digital Equipment Corp. pp B-1.
  45.  *
  46.  *  RESTRICTIONS
  47.  *
  48.  *      The maximum relative error for the approximating polynomial
  49.  *      in atan is 10**(-16.82).  However, this assumes exact arithmetic
  50.  *      in the polynomial evaluation.  Additional rounding and
  51.  *      truncation errors may occur as the argument is reduced
  52.  *      to the range over which the polynomial approximation
  53.  *      is valid, and as the polynomial is evaluated using
  54.  *      finite-precision arithmetic.
  55.  *      
  56.  *  PROGRAMMER
  57.  *
  58.  *      Fred Fish
  59.  *
  60.  *  INTERNALS
  61.  *
  62.  *      Computes arccosine(x) from:
  63.  *
  64.  *              (1)     If x < -1.0  or x > +1.0 then call
  65.  *                      matherr and return 0.0 by default.
  66.  *
  67.  *              (2)     If x = 0.0 then acos(x) = PI/2.
  68.  *
  69.  *              (3)     If x = 1.0 then acos(x) = 0.0
  70.  *
  71.  *              (4)     If x = -1.0 then acos(x) = PI.
  72.  *
  73.  *              (5)     If 0.0 < x < 1.0 then
  74.  *                      acos(x) = atan(Y)
  75.  *                      Y = sqrt[1-(x**2)] / x 
  76.  *
  77.  *              (4)     If -1.0 < x < 0.0 then
  78.  *                      acos(x) = atan(Y) + PI
  79.  *                      Y = sqrt[1-(x**2)] / x 
  80.  *
  81.  */
  82.  
  83. #include <stdio.h>
  84. #include "pml.h"
  85.  
  86. static char funcname[] = "acos";
  87.  
  88. double acos (x)
  89. double x;
  90. {
  91.     double y;
  92.     extern double atan();
  93.     extern double sqrt();
  94.     struct exception xcpt;
  95.     
  96.     DBUG_ENTER (funcname);
  97.     DBUG_3 ("acosin", "arg %le", x);
  98.     if ( x > 1.0 || x < -1.0) {
  99.         xcpt.type = DOMAIN;
  100.         xcpt.name = funcname;
  101.         xcpt.arg1 = x;
  102.         if (!matherr (&xcpt)) {
  103.             fprintf (stderr, "%s: DOMAIN error\n", funcname);
  104.             errno = EDOM;
  105.             xcpt.retval = 0.0;
  106.         }
  107.     } else if (x == 0.0) {
  108.         xcpt.retval = HALFPI;
  109.     } else if (x == 1.0) {
  110.         xcpt.retval = 0.0;
  111.     } else if (x == -1.0) {
  112.         xcpt.retval = PI;
  113.     } else {
  114.         y = atan ( sqrt (1.0 - (x * x)) / x );
  115.         if (x > 0.0) {
  116.             xcpt.retval = y;
  117.         } else {
  118.             xcpt.retval = y + PI;
  119.         }
  120.     }
  121.     DBUG_3 ("acosout", "result %le", x);
  122.     DBUG_RETURN (x);
  123. }
  124.